package src;

import java.util.Arrays;
import java.util.Scanner;

public class BinarySearchTest {
    public static void main(String[] args) {
        BinarySearch bs = new BinarySearch();
        int[] a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入基本二分查找的目标值：");
        int target1 = scanner.nextInt();
        System.out.println("基本二分查找方法：" + bs.binarySearchBasic(a, target1));

        // 替代二分查找方法
        System.out.println("请输入替代二分查找的目标值：");
        int target2 = scanner.nextInt();
        System.out.println("替代二分查找方法：" + bs.binarySearchAlternative(a, target2));

        // 平衡二分查找方法
        System.out.println("请输入平衡二分查找的目标值：");
        int targetBalance = scanner.nextInt();
        System.out.println("平衡二分查找方法：" + bs.binarySearchBalance(a, targetBalance));

        // 左边界查找方法 1
        System.out.println("请输入左边界查找方法 1 的目标值：");
        int targetLeftmost1 = scanner.nextInt();
        System.out.println("左边界查找方法 1：" + bs.binarySearchLeftmost1(a, targetLeftmost1));

        // 右边界查找方法 1
        System.out.println("请输入右边界查找方法 1 的目标值：");
        int targetRightmost1 = scanner.nextInt();
        System.out.println("右边界查找方法 1：" + bs.binarySearchRightmost1(a, targetRightmost1));

        // 左边界查找方法 2
        System.out.println("请输入左边界查找方法 2 的目标值：");
        int targetLeftmost2 = scanner.nextInt();
        System.out.println("左边界查找方法 2：" + bs.binarySearchLeftmost2(a, targetLeftmost2));

        // 右边界查找方法 2
        System.out.println("请输入右边界查找方法 2 的目标值：");
        int targetRightmost2 = scanner.nextInt();
        System.out.println("右边界查找方法 2：" + bs.binarySearchRightmost2(a, targetRightmost2));



//           ↓
//        [2,5,8]    a
//        [2,0,0,0]  b
//        [2,4,0,0]  b
//        [2,4,5,8]  b

        int[] a2 = {2, 5, 8};
        int target3 = 4;

        int i = Arrays.binarySearch(a2, target3);
        System.out.println(i);
        //-2 = -插入点-1
        //-2+1=-插入点

        if (i < 0) {
            int insertIndex = Math.abs(i + 1);//插入点索引
            int[] b = new int[a2.length + 1];
            System.arraycopy(a2, 0, b, 0, insertIndex);
            b[insertIndex] = target3;
            System.arraycopy(a2, insertIndex, b, insertIndex + 1, a2.length - insertIndex);
            System.out.println(Arrays.toString(b));
        }
    }
}
